<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- 组件的使用 -->
        <button-counter></button-counter>
        <button-counter></button-counter>
        <button-counter></button-counter>
        <button-counter1></button-counter1>
    </div>
    <script src="../js/vue.min.js"></script>
    <script>
        Vue.component('button-counter', {
            // 1.内部数据必须使用函数,形成一个闭包环境
            data: function() {
                return {
                    count: 0
                }
            },
            //2. 组件模板的内容必须是单个根元素,不能是两个兄弟元素
            template: '<button @click="handle">点击了{{count}}次</button>',
            methods: {
                handle: function() {
                    this.count++
                }
            }
        });
        Vue.component('button-counter1', {
            data: function() {
                return {
                    res: 0
                }
            },
            //3. 组件模板的内容可以是模板字符串,但是某些浏览器不支持
            template: `
            <div>
                <button @click="handle">点击了{{res}}次</button>
                <button>测试</button>
                <ButtonCounter></ButtonCounter>
            </div>`,
            methods: {
                handle: function() {
                    this.res++
                }
            }
        });

        //4. 组件名称有驼峰式和连字符式两种
        // 驼峰式用于模板字符串中,不能直接使用在根组件中
        // 必须在根组件使用,必须换成连字符式
        Vue.component('ButtonCounter', {

            data: function() {
                return {
                    count: 0
                }
            },

            template: '<button @click="handle">点击了{{count}}次</button>',
            methods: {
                handle: function() {
                    this.count++
                }
            }
        });



        let vm = new Vue({
            el: "#app",
            data: {

            }
        })
    </script>
</body>

</html>